<template>
  <div class="container">
    <el-dialog
      title="商品搜索"
      :visible.sync="dialog.centerDialogVisible"
      width="1200px"
      center
      @open="getData"
      :lock-scroll="true"
    >
      <div class="top">
        <el-input style="width:300px;" placeholder="请输入价格" v-model="search.value">
          <template slot="prepend">价格</template>
        </el-input>
      </div>
      <el-table
        :data="tableData"
        max-height="450px"
        border
        style="width:100%;margin-top:10px"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
        empty-text="暂无数据"
      >
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column
          prop="suppliser_info.name"
          label="供应商名称"
          width="180"
          align="center"
        ></el-table-column>
        <el-table-column prop="erp_goods_code" label="商品code" width="100" align="center"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="price" label="商品价格" width="80" align="center"></el-table-column>
        <el-table-column prop="stock_num" label="剩余数量" width="80" align="center"></el-table-column>
        <el-table-column label="操作数量" width="150" align="center">
          <template slot-scope="scope">
            <el-input
              :controls="false"
              v-model="scope.row.stock_num_change"
              size="mini"
              :min="0"
              controls-position="right"
              :precision="2"
              :step="1"
              @change="aliasName(scope)"
              placeholder="请输入操作数量"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="stock_qty_change" label="件数" width="80" align="center"></el-table-column>
        
        <!-- <el-table-column label="操作件数" width="150" align="center">
          <template slot-scope="scope">
            <el-input-number
              :controls="false"
              v-model="scope.row.stock_qty_change"
              size="mini"
              :min="0"
              controls-position="right"
              :precision="2"
              :step="1"
              @change="taxrate(scope)"
              placeholder="请输入操作箱数"
            ></el-input-number>
          </template>
        </el-table-column>-->
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">高退低进</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  Changeprice,
  ChangepricestockShowlist,
} from "@/api/stockHeight/stockhei";

export default {
  props: {
    dialog: Object,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      search: {
        value: "",
      },
      tags: [],
    };
  },
  methods: {
    getData() {
      // this.search.value = this.dialog.numprice;
      this.tableData = [];
      let data = {
        erp_goods_code: this.dialog.id,
        dccode_str:this.dialog.selectList

      };
      ChangepricestockShowlist(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.forEach((res) => {
              res.stock_num_change = res.stock_num;
              res.stock_qty_change = (res.stock_num / res.rk_spec).toFixed(4);
              res.nums = res.stock_num;
            });
            this.tableData = res.data;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    //修改操作数量
    aliasName(scope) {
      this.$refs.multipleTable.clearSelection();
      this.tags = [];
      if (parseFloat(scope.row.stock_num_change) > parseFloat(scope.row.nums)) {
        scope.row.stock_num_change = scope.row.nums;
        scope.row.stock_qty_change =
          (scope.row.stock_num_change / scope.row.rk_spec).toFixed(4);
        this.$set(this.tableData, scope.$index, scope.row);
        this.$message({
          type: "info",
          message: "不能大于默认数量",
        });
      }
      scope.row.stock_qty_change =
        (scope.row.stock_num_change / scope.row.rk_spec).toFixed(4);
      this.$set(this.tableData, scope.$index, scope.row);
    },
    //修改操作箱数
    taxrate(scope) {
      this.$refs.multipleTable.clearSelection();
      this.tags = [];

      // scope.row.stock_qty
    },
    //确定
    confirm() {
      if (!this.search.value) {
        this.$message({
          message: "请填写价格",
          type: "warning",
        });
        return;
      }
      if (this.tags !== undefined && this.tags.length > 0) {
      } else {
        this.$message({
          message: "请选择后操作",
          type: "warning",
        });
        return;
      }
      let data = {
        change_price_data: JSON.stringify(this.tags),
        change_price: this.search.value,
      };
      Changeprice(data)
        .then((res) => {
          if (res.code == 0) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.cancel();
            this.$emit("upData");
          } else {
            this.$message({
              message: res.msg,
              type: "warning",
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });

      // this.$emit("tag", this.tags);
    },
    //取消
    cancel() {
      this.search.value = "";
      this.tableData = [];
      this.$refs.multipleTable.clearSelection();
      this.dialog.centerDialogVisible = false;
    },

    //选取
    handleSelectionChange(data) {
      this.tags = [];
      data.forEach((res) => {
        this.tags.push({
          change_num: res.stock_num_change,
          change_qty: res.stock_qty_change,
          id: res.id,
        });
      });
    },
  },
};
</script>
<style scoped>
.top {
  display: flex;
  /* justify-content: center; */
}
</style>